﻿namespace Beetle
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Text;

    public abstract class Package : IDisposable
    {
        private Class_ae _field_a;
        private PacketRecieveMessagerArgs _field_b;
        internal Class_ag _field_c;
        private BufferWriter _field_d;
        private bool _field_e;
        [CompilerGenerated]
        private Encoding _field_f;
        public TcpChannel Channel;
        protected BufferReader Reader;
        public EventPacketRecievMessage ReceiveMessage;
        protected BufferWriter Writer;

        public Package()
        {
            this._field_a = Class_ae.f();
            this._field_b = new PacketRecieveMessagerArgs(null, null);
            this.Reader = TcpUtils.BufferReaderPool.Pop();
            this.Coding = Encoding.UTF8;
        }

        public Package(TcpChannel channel)
        {
            this._field_a = Class_ae.f();
            this._field_b = new PacketRecieveMessagerArgs(null, null);
            this.Reader = TcpUtils.BufferReaderPool.Pop();
            channel._field_h = this;
            this.Channel = channel;
            this.Coding = channel.Coding;
            this._field_c = channel._field_m;
            channel.DataReceive = new EventDataReceive(this.ImportReceiveData);
            channel.MessageWriting = new EventMessageWriter(this.MessageWrite);
            channel.MessageWrited = new EventMessageWriter(this.MessageWrited);
            this._field_b.Channel = channel;
            this.CreateWriterReader();
        }

        internal void a()
        {
            this.Channel.CloseStatus = "package reset!";
            this.Channel.Dispose();
        }

        internal void a(IMessage A_0, object A_1)
        {
            try
            {
                this._field_b.Message = this.ReadCast(A_0);
                this.OnReceiveMessage(this._field_b);
            }
            catch (Exception exception)
            {
                throw new LogicException(exception, "message handler error!");
            }
        }

        public static int ByteIndexOf(byte[] searched, byte[] find)
        {
            return ByteIndexOf(searched, find, 0);
        }

        public static int ByteIndexOf(byte[] searched, byte[] find, int start)
        {
            return ByteIndexOf(searched, find, start, searched.Length - start);
        }

        public static int ByteIndexOf(byte[] searched, byte[] find, int start, int count)
        {
            bool flag = false;
            int index = find.Length - 1;
            int num2 = 0;
            int num3 = (start + count) - find.Length;
            for (int i = start; i <= num3; i++)
            {
                num2 = 0;
                flag = true;
                if ((find[0] == searched[i]) && (find[index] == searched[i + index]))
                {
                    num2++;
                    if (index > 10)
                    {
                        if ((find[num2] != searched[i + num2]) || (find[index - num2] != searched[(i + index) - num2]))
                        {
                            continue;
                        }
                        num2++;
                    }
                    for (int j = num2; j < (find.Length - num2); j++)
                    {
                        if (find[j] != searched[i + j])
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        return ((i + find.Length) - 1);
                    }
                }
            }
            return -1;
        }

        public void CreateWriterReader()
        {
            this.Writer = new BufferWriter(this.Coding, this._field_c, false);
            this._field_d = new BufferWriter(this.Coding, this._field_c, false);
            if (this.Channel != null)
            {
                this._field_d.EnabledVariant = this.Channel.EnabledVariant;
                this.Writer.EnabledVariant = this.Channel.EnabledVariant;
                this.Reader.EnabledVariant = this.Channel.EnabledVariant;
                if (!this._field_a.f(this.Channel._field_d))
                {
                    this.a();
                }
            }
        }

        public void Dispose()
        {
            lock (this)
            {
                if (!this._field_e)
                {
                    this._field_e = true;
                    this.OnDisposed();
                }
            }
        }

        public void GetPools()
        {
            this._field_c = Class_ag.b();
        }

        public abstract void Import(byte[] data, int start, int count);
        public void ImportReceiveData(object sender, ChannelReceiveEventArgs e)
        {
            this.Import(e.Data.Array, 0, e.Data.Count);
        }

        public abstract IMessage MessageRead(BufferReader reader);
        public void MessageToArraySegment(object msg, ByteArraySegment data)
        {
            this._field_d.Reset();
            this.MessageWrite((IMessage) this.WriteCast(msg), this._field_d);
            data.Import(this._field_d);
        }

        public abstract void MessageWrite(IMessage msg, BufferWriter writer);
        public virtual void MessageWrited(IMessage msg, BufferWriter writer)
        {
        }

        protected virtual void OnDisposed()
        {
            if (this.Writer != null)
            {
                this.Writer.Dispose();
            }
            if (this._field_d != null)
            {
                this._field_d.Dispose();
            }
            if (this.Reader != null)
            {
                this.Reader.CleanBuffer();
                TcpUtils.BufferReaderPool.Push(this.Reader);
                this.Reader = null;
            }
            this._field_c = null;
            this._field_b.Channel = null;
            this._field_b.Message = null;
            this._field_b = null;
            this.ReceiveMessage = null;
            this.Channel = null;
        }

        protected virtual void OnMessageDataReader(BufferWriter writer)
        {
            lock (TcpUtils._field_i)
            {
                TcpUtils.ReceiveMessages += 1L;
            }
            IMessage message = null;
            this.Reader.Coding = this.Coding;
            try
            {
                this.Reader.Import(writer);
                message = this.MessageRead(this.Reader);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                this.Reader.CleanBuffer();
            }
            this.a(message, null);
        }

        protected virtual void OnReceiveMessage(PacketRecieveMessagerArgs e)
        {
            if (this.ReceiveMessage != null)
            {
                this.ReceiveMessage(e);
            }
        }

        public virtual object ReadCast(object message)
        {
            return message;
        }

        public virtual object WriteCast(object message)
        {
            return message;
        }

        public Encoding Coding
        {
            [CompilerGenerated]
            get
            {
                return this._field_f;
            }
            [CompilerGenerated]
            set
            {
                this._field_f = value;
            }
        }
    }
}

